/**
 * 这是一个存进本地lacalStorage的函数
 * @param {String | Number} name 用户名
 * @param {Number} val   玩家玩完游戏的数量 
 * @returns {null}
 */
function setdata(name,val) {
    let dataarr = JSON.parse(localStorage.getItem("blockData"));
    if (dataarr == '' || dataarr == null || dataarr == undefined) {
        let arr = [
            {
                "username":name,
                "gamenub":[val]
            }
        ]
        localStorage.setItem("blockData",JSON.stringify(arr));
    }else {
        for (let i in dataarr) {
            if (dataarr[i]['username'] == name) {
                dataarr[i]['gamenub'].push(val);
                localStorage.setItem("blockData",JSON.stringify(dataarr));
                return;
            }else if (dataarr[i]['username'] != name &&  i == dataarr.length - 1){
                dataarr.push({
                    "username":name,
                    "gamenub":[val]
                })
                localStorage.setItem("blockData",JSON.stringify(dataarr));
            }
        }
    }   
    
}
/**
 * 对数组进行排序然后返回最大值
 * @param {Array} arr 
 * @returns {Array}
 */
function Arrmax(arr) {
    if (arr.length = 0 ) {
        return false;
    }else  {
        let list = arr.sort((a,b) => b -a);
        return list[0];
    }
}

/**
 * 把传进来的数组里面的值给加入进去
 * @param {Array} arr 
 */
function shici_show(arr) {
    $("#resultsul").html('');
    $("#resultsul").append(`<li>游戏成绩</li><li>分数</li>`)
    for (let i in arr) {
        $("#resultsul").append(`<li>No.${Number(i)+1}</li><li style="color:#fff">${arr[i]}分</li>`);
    }
}

/**
 * 对用户的数据做排序等处理
 * @param {String} name 
 */
function topThree(name) {
    let dataarr = JSON.parse(localStorage.getItem("blockData"));
    for (let i in dataarr) {
        if (dataarr[i]['username'] == name) {
            let list = dataarr[i]['gamenub'];
            list = list.sort((a,b) => b-a).slice(0,10);
            dataarr[i]['gamenub'] = list;
            localStorage.setItem("blockData",JSON.stringify(dataarr));
            shici_show(list);
        }
    }
}

/**
 * 显示排行榜代码
 */
function Threeshow() {
    let arr = [];
    let arrs = [];
    let dataarr = JSON.parse(localStorage.getItem("blockData"));
    for (let i in dataarr) {
        let list = dataarr[i]['gamenub'].sort((a,b) => b-a);
        if (dataarr[i]['username'] && list){
            arr.push(dataarr[i]['username']);
            arrs.push(list[0]); 
        }   
    }
    let penlist = [];
    for (let i in arrs) {
        penlist.push(arrs[i]);
    }
    let penlists = penlist.sort((a,b) =>b-a);
    if (arr[arrs.indexOf(penlists[0])]) {
        $("#paiming .pai1").text(arr[arrs.indexOf(penlists[0])]);
        $("#paiming .pai1").parent().next().text(penlists[0] +'分');
    }
    if (arr[arrs.indexOf(penlists[1])]) {
        $("#paiming .pai2").text(arr[arrs.indexOf(penlists[1])]);
        $("#paiming .pai2").parent().next().text(penlists[1] +'分');
    }
    if (arr[arrs.indexOf(penlists[2])]) {
        $("#paiming .pai3").text(arr[arrs.indexOf(penlists[2])]);
        $("#paiming .pai3").parent().next().text(penlists[2] +'分');
    }
    
}

